package com.example.demo.connect;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import com.example.demo.util.EmptyUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * webservice客户端
 * @author admin
 *
 */
@Slf4j
public class WSConnect {
	
	//调用webservice接口
	public static String callTrans(String wsdl,String namespace,String methodname,String param) throws Exception{
		Service service = new Service();
		
		Call call = (Call)service.createCall();
		
		call.setTargetEndpointAddress(wsdl);
		
		call.setOperationName(new QName(namespace, methodname));
		
		if(EmptyUtil.isNotEmpty(param)) {
			call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN);
		}
		
		call.setReturnType(XMLType.XSD_STRING);
		
		Object[] paramObj = null;
		
		if(EmptyUtil.isNotEmpty(param)) {
			paramObj = new Object[] {param};
		}
		
		String result = (String)call.invoke(paramObj);
		
		log.debug("webservice响应数据:{}",result);
		
		return result;
	}
	
	public static void main(String[] args) {
		try {//
//			callTrans("http://192.168.100.162:10001/wstest", "http://service.xzw/", "callService", "{\"name\":\"许子文\",\"send\":\"你好\"}");
			callTrans("http://192.168.100.162:10001/wstest?wsdl", "http://service.xzw/", "callService", "{\"name\":\"xuziwen\"}");
		
			
//			System.out.println("result="+result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
